<?php
/********************
 *@param function:積分變更記錄 model
 *@param datetime 2011-6-7
 *@param author abin
 ********************/
class Integralhistory_mdl extends CI_model{
  //操作的表.
  const TAB_NAME='clickrcms_integral_history';
  function __construct(){
    parent::__construct();
    log_message('debug','integralhistory_mdl Model class Initialized');
  }

  /****
   *查詢所integralhistory,加分页
   *$data :查询参数.
   */
  public function get_integralhistory($data=array()){
    if(!empty($data)){
      extract($data);
      isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('add_time DESC');
      isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
      isset($integral_id) && !empty($integral_id)?$this->db->where('integral_id',$integral_id):'';
      isset($notify) && !empty($notify)?$this->db->where('notify',$notify):'';
      isset($admin_name) && !empty($admin_name)?$this->db->where('admin_name',$admin_name):'';
      isset($begin_month) && !empty($begin_month) && isset($end_month) && !empty($end_month)?$this->db->where('send_date BETWEEN "'.$begin_month.'" AND "'.$end_month.'"',NULL,FALSE):'';//allocation,index
      return $this->db->get(self::TAB_NAME);
    }
  }
  /****
   * 統計所有的integralhistory
   */
  public function totle_integralhistory($data=array()){
    if(!empty($data)){
      extract($data);
      $this->db->order_by('add_time DESC');
      isset($integral_id) && !empty($integral_id)?$this->db->where('integral_id',$integral_id):'';
      isset($notify) && !empty($notify)?$this->db->where('notify',$notify):'';
      isset($admin_name) && !empty($admin_name)?$this->db->where('admin_name',$admin_name):'';
      isset($begin_month) && !empty($begin_month) && isset($end_month) && !empty($end_month)?$this->db->where('send_date BETWEEN "'.$begin_month.'" AND "'.$end_month.'"',NULL,FALSE):'';//allocation,index
      return $this->db->count_all_results(self::TAB_NAME);
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  /*****
   * 根據ID來刪除
   */
  public function delete_integralhistory($delete_id=0){
    if(!empty($delete_id)){
      $this->db->where("id",$delete_id);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /*****
   * 根據ID數組來刪除 批量刪除
   */
  public function delete_batch($batch=array()){
    if(!empty($batch)){
      $this->db->where_in("id",$batch);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /****
   *根据ID来查询得到一个信息.
   *$id :要查詢的ID
   */
  public function get_integralhistory_by_id($id){
    $data=array();
    $this->db->where('id',$id);
    $get_result=$this->db->get(self::TAB_NAME);
    if($get_result->num_rows()>0)
    {
      $data=$get_result->row_array();
    }
    $get_result->free_result();
    return $data;
  }
  /***
   * 修改一個integralhistory
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_integralhistory($data=array(),$b_id=0){
    if(!empty($data) && !empty($b_id)){
      $this->db->where('id',$b_id);
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 新增一個 integralhistory
   */
  public function add_integralhistory($data=array()){
    if(!empty($data)){
      $this->db->insert(self::TAB_NAME,$data);
      return $this->db->insert_id();
    }
  }
}